<!-- 落地页配置编辑弹窗 -->
<template>
  <el-dialog
    :title="'落地页配置'"
    :visible="visible"
    width="67s0px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="220px">
      <el-form-item label="落地页模板:" prop="package_landing_template_id">
        <PageSelect
          :placeholder="'请选择落地页模板'"
          v-model="form.package_landing_template_id"
          :whereName="'name'"
          :url="'/Packagelandingtemplate/index'"
          :optionLable="'name'"
          :optionvalue="'id'"
          :multiple="false"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import PageSelect from "@/components/pageSelect/index.vue";
export default {
  name: "PackagelandingPage",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {PageSelect},
  data() {
    return {
      // 表单数据
      form: Object.assign({}),
      // 表单验证规则
      rules: {
        package_landing_template_id: [
          { required: true, message: "请选择落地页模板", trigger: "blur" }
        ]
      },

      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      packageId: "",
      save_form: {}
      // sdk_version_guanlian: ""
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
        console.log(this.form,"fff");
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    },
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs["form"].validate(valid => {
        console.log(this.form, 'ffff')

        this.save_form.id = this.form.id;

        this.save_form.package_landing_template_id = this.form.package_landing_template_id;

        console.log(this.save_form, 'ffff222')
        // console.log(valid,'!');
        if (valid) {
          this.loading = true;
          this.form.package_id = this.form.id;
          this.$http
            .post("/Apply/setPackageLandingTemplate", this.save_form)
            .then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
      // console.log(this.form, '!!')
    }
  }
};
</script>

<style scoped></style>
